package com.dmyang.controller;

import com.dmyang.common.pojo.BootstrapTableResult;
import com.dmyang.common.pojo.HyResult;
import com.dmyang.entity.HyCategory;
import com.dmyang.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 分类管理Controller
 * Created by brss on 2017/8/2.
 */
@Controller
@RequestMapping("category")
public class CategoryController {

    @Autowired
    private CategoryService categoryService;

    /**
     * 列出所有分类（分页）
     * @param offset 从第多少条开始显示
     * @param limit 每页显示的条数
     * @return 要求格式的对象
     */
    @RequestMapping("/list")
    @ResponseBody
    public BootstrapTableResult showCategoryList(Integer offset,Integer limit){
        BootstrapTableResult result = categoryService.showCategoryList(offset, limit);
        return result;
    }

    /**
     * 添加一条分类
     * @param category
     * @return
     */
    @RequestMapping("/add")
    @ResponseBody
    public HyResult addCategory(HyCategory category){
        try {
            HyResult result = categoryService.addCategory(category);
            return result;
        }catch (Exception e){
            e.printStackTrace();
            return HyResult.build(500,"插入失败!");
        }
    }

    /**
     * 修改一条分类
     * @param category
     * @return
     */
    @RequestMapping("/modify")
    @ResponseBody
    public HyResult modifyCategory(HyCategory category){
        try {
            HyResult result = categoryService.modifyCategory(category);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            return HyResult.build(500,"修改失败！");
        }
    }
    @RequestMapping("/delete")
    @ResponseBody
    public HyResult deleteCategory(String ids){
        try {
            HyResult result = categoryService.deleteCategory(ids);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            return HyResult.build(500,"删除失败！");
        }
    }

}
